/**
 * Copyright (c) 2010 sonntagsfarben.de
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package de.sonntagsfarben.puremvc.as3.proxy
{
	import br.com.stimuli.loading.BulkLoader;
	import br.com.stimuli.loading.BulkProgressEvent;
	import br.com.stimuli.loading.loadingtypes.LoadingItem;

	import flash.events.ErrorEvent;
	import flash.events.ProgressEvent;

	import org.puremvc.as3.multicore.patterns.proxy.Proxy;

	/**
	 * Short description for file.
	 *
	 * @package    de.sonntagsfarben.puremvc.as3.proxy
	 * @author     Soenke Kluth <sonntagsfarben.de> / 2010
	 * @version    SVN: $Id$
	 */

	public class BulkLoaderProxy extends Proxy
	{

		public static const NAME:String = "BulkLoaderProxy";
		public static const PRELOADER:String = "PRELOADER";
		public static const RUNTIMELOADER:String = "RUNTIMELOADER";

		//TODO die frage nach dem NAME wegen der notifications klären!"!!		
		public static const LOAD_PROGRESS:String = NAME + "LOAD_PROGRESS";
		public static const LOAD_COMPLETE:String = NAME + "LOAD_COMPLETE";
		public static const LOAD_START:String = NAME + "LOAD_START";
		public static const ITEM_ERROR:String = NAME + "ITEM_ERROR";

		private var _bulkloader:BulkLoader;
		private var _completeHandler:Function;


		public function BulkLoaderProxy(name:String = BulkLoaderProxy.NAME)
		{
			super(name, null);
			_initLoader();
		}


		/****************************************
		 *			PRIVATE / PROTECTED
		 ****************************************/

		private function _initLoader():void
		{
			if (BulkLoader.getLoader(getProxyName()) != null)
			{
				throw new Error(getProxyName() + " is allready in use!!");
			}

			_bulkloader = new BulkLoader(getProxyName());
			_bulkloader.addEventListener(BulkLoader.PROGRESS, onProgress);
			_bulkloader.addEventListener(BulkLoader.COMPLETE, onLoadComplete);
			_bulkloader.addEventListener(BulkLoader.ERROR, onError);
			_bulkloader.addEventListener(BulkLoader.SECURITY_ERROR, onError);

		}




		/****************************************
		 *			EVENT-HANDLER
		 ****************************************/

		private function onProgress(event:BulkProgressEvent):void
		{
			var percent:Number = _bulkloader.weightPercent;
			sendNotification(BulkLoaderProxy.LOAD_PROGRESS, percent);
			sendNotification(BulkLoaderProxy.LOAD_PROGRESS, (event.target as BulkLoader));
		}


		protected function onLoadComplete(event:ProgressEvent):void
		{
			_completeHandler();
			try
			{
				sendNotification(LOAD_COMPLETE, _bulkloader);
			}
			catch (e:Error)
			{
				trace(this + " " + getProxyName() + " loading is complete but proxy wasn't registered yet");
			}
		}


		protected function onError(event:ErrorEvent):void
		{
			sendNotification(ITEM_ERROR, (event.target as LoadingItem));
		}




		override public function onRegister():void
		{
		}


		override public function onRemove():void
		{
		}


		/****************************************
		 *			PUBLIC
		 ****************************************/


		public function addItem(url:String, id:String, type:String = null, weight:int = 0, priority:int = 0, context:Object = null, maxTries:uint = 1, preventCache:Boolean = false, pausedAtStart:Boolean = true, checkPolicyFile:Boolean = false, headers:Array = null):void
		{
			var props:Object = {id: id, type: type, weight: weight, priority: priority, context: context, maxTries: maxTries, preventCaching: preventCache, pausedAtStart: pausedAtStart, checkPolicyFile: checkPolicyFile, headers: headers};
			_bulkloader.add(url, props);
		}


		public function getItem(itemId:String):LoadingItem
		{
			return _bulkloader.get(itemId);
		}


		public function getContent(id:String, clearMemory:Boolean = false):*
		{
			return _bulkloader.getContent(id, clearMemory);
		}


		public function start(completeHandler:Function = null):void
		{
			_completeHandler = completeHandler;
			_bulkloader.start();
		}


		public function clear():void
		{
			_bulkloader.clear();
		}


		/****************************************
		 *			GETTER-SETTER
		 ****************************************/

		public function get bulkloader():BulkLoader
		{
			return _bulkloader;
		}
	}
}